$(document).ready(function(){
	var $form = $('#b-a-form');
		$pic = $('.pic'),
		$preImg = $('.pre-img'),
		$uploadBtn = $('.upload-btn');
		isUpload = false;
		uploadImgUrl = 'book.jpg';
		uploadImgName = 'book.jpg',
		tipsPic = $('.modal .tips-pic')[0],
		$tips = $('.modal .tips-content'),
		$dialogBox = $('#dialog-box');
		$modalFooter = $('.modal-footer');
		//表单字段
		$isbn = $('#isbn');
		$author = $('#author');
		$bname =$('#bname');
		$publisher =$('#publisher');
		$type = $('#type');
		//提交
		$submit = $('#submit');
	$pic.change(function(){
		var file = this.files ;
		if(file[0].size>1.5*1024*1024){
			$tips.html('图片太大啦');
			tipsPic.className="tips-pic pic-warn";
			$dialogBox.modal({
				backdrop:false,
				keyboard:false
			});
		}else{
			var xhr = new XMLHttpRequest();
			 var fd = new FormData();
	       //关联表单数据,可以是自定义参数
	       fd.append("name", file.name);
	       fd.append("fileName", this.files[0]);
	       xhr.addEventListener("load", uploadComplete, false);
	       $pic.attr('disabled','disabled');
	       $uploadBtn.html('<span class="loading-pic"></span>');
	       //发送文件和表单自定义参数
	       xhr.open("POST", "/bookcrossing/upload");
	       xhr.send(fd);
		}
	});
	//表单提交
	$form.submit(function(e){
		e.preventDefault();
		var tipStr = '',
			$where = $('');
		if($.trim($isbn.val())==''){
			tipStr = 'isbn不能空哦';
			$where = $isbn;
		}else if($.trim($bname.val())==''){
			tipStr = '书名不能空哦';
			$where = $bname;
		}else if($.trim($author.val())==''){
			tipStr = '作者不能空哦';
			$where = $author;
		}else if($.trim($type.val())=='请选择...'){
			tipStr = '请选择一个分类哦';
			$where = $type;
		}else if($.trim($publisher.val())==''){
			tipStr = '出版社不能空哦';
			$where = $publisher;
		}else if(!isUpload){
			tipStr = '要上传一个封面哦';
		}
		if(tipStr!=''){
			$tips.html(tipStr);
			tipsPic.className="tips-pic pic-warn";
			$dialogBox.modal({
				backdrop:false,
				keyboard:false
			});
			$where.focus();
		}else{
			BC.ajax({
				type:'POST',
				url:'/bookcrossing/book/add',
				data:$form.serialize()+'&picurl='+uploadImgUrl+'&picname='+uploadImgName,
				b:function(){
					$submit.html('<span class="loading-pic"></span>').attr('disabled','disabled');
				},
				s:function(resp){
					$modalFooter.html('<button type="button" class="btn btn-info btn-sm ohyes">是滴</button>' +
					'<button type="button" class="btn btn-warning btn-sm ohno">不了</button>');
					$tips.html(resp.msg);
					tipsPic.className="tips-pic pic-success";
					//绑定发布成功的按钮事件
					$('.ohyes').on('click',document,function(){
						window.location.reload();
					});
					$('.ohno').on('click',document,function(){
						window.location.href = BC.getRootPath();
					});
					$dialogBox.modal({
						backdrop:false,
						keyboard:false
					});
				},
				f:function(resp){
					$tips.html(resp.msg);
					tipsPic.className="tips-pic pic-warn";
					$dialogBox.modal({
						backdrop:false,
						keyboard:false
					});
				},
				e:function(){
					$tips.html('哎呀，网络不好呀，换个姿势再试一试');
					tipsPic.className="tips-pic pic-warn";
					$dialogBox.modal({
						backdrop:false,
						keyboard:false
					});
				},
				c:function(){
					$submit.html('填好啦').removeAttr('disabled');
				}
			});
		}
	});
	//图片上传后的事件
	function uploadComplete(ev){
		if(ev.target.responseText){
			var data = JSON.parse(ev.target.responseText);
			if(data.success==true){
				BC.createPreImg($pic[0],$preImg[0]);
				isUpload = true ;
				uploadImgUrl = data.url;
				uploadImgName = data.name;
			}else{
				$tips.html('上传失败了');
				tipsPic.className="tips-pic pic-warn";
				$dialogBox.modal({
					backdrop:false,
					keyboard:false
				});
			}
			$pic.removeAttr('disabled');
	    	$uploadBtn.html('<span class="glyphicon glyphicon-upload"></span> 重新上传');
		}
	}
});